home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / EEV100R1.ARJ / DFSTR.PAS < prev    next >
Pascal/Delphi Source File  |  1991-12-29  |  4KB  |  145 lines

  1. Unit DFStr;
  2.  
  3. { ----------------------------------------------------------------------
  4.   DFSTR.PAS - string functions for Turbo Pascal
  5.  
  6.   Written 1991 by:
  7.  
  8.   David Firth                           GEnie: D.FIRTH
  9.   5665 A2 Parkville Ct.                   CIS: 76467,1734
  10.   Columbus, OH 43229
  11.   (614) 523-1968
  12.  
  13.   Revision 0: May 1991 - __JustStr & __RemWhiteStr for TP3
  14.   Revision 1: Dec 1991 - Made file into a unit for TP5 or above
  15.  
  16.   ---------------------------------------------------------------------- }
  17.  
  18. Interface
  19.  
  20. Type
  21.  
  22.   Str8   = string[8];
  23.  
  24. Const
  25.  
  26.   _LeftJustify  : byte = 0;               {__JustStr}
  27.   _RightJustify : byte = 1;               {__JustStr}
  28.   _Leading      : byte = 0;               {__RemWhiteStr}
  29.   _Trailing     : byte = 1;               {__RemWhiteStr}
  30.   _All          : byte = 2;               {__RemWhiteStr}
  31.  
  32. Function __RemWhiteStr(InputStr:string;MyMode:byte):string;
  33.  
  34. Function __JustStr(InputStr:string;
  35.                    Len:byte;
  36.                    JustChar:char;
  37.                    JustMode:byte) : string;
  38.  
  39. Implementation
  40.  
  41. { ------------------------ PROCEDURES/FUNCTIONS ----------------------- }
  42.  
  43. Function __JustStr(InputStr:string;
  44.                    Len:byte;
  45.                    JustChar:char;
  46.                    JustMode:byte) : string;
  47.  
  48. { Justify string is a very useful function for padding strings. After
  49.   the first time I used a justify function, I had to hae one for TP3.
  50.  
  51.   InputStr is the string to pad.
  52.  
  53.   Len is the length that the final string needs to be.
  54.  
  55.   JustChar is the character to pad by.
  56.  
  57.   JustMode is a constant, chosen from the list above, which chooses
  58.   whether the string is left or right justified.
  59. }
  60.  
  61. Var
  62.  
  63.   Count,                                   {general purpose counter         }
  64.   NumChars   : byte;                       {number of characters to pad with}
  65.  
  66. Begin
  67.  
  68.   if Length(InputStr) < Len then begin
  69.  
  70.     NumChars := Len - Length(InputStr);
  71.     for Count := 1 to NumChars do begin
  72.       case JustMode of
  73.         0: InputStr := InputStr + JustChar;
  74.         1: InputStr := JustChar + InputStr;
  75.       end; {case}
  76.     end; {for}
  77.  
  78.   end
  79.   else begin
  80.  
  81.     InputStr := Copy(InputStr,1,Len);
  82.  
  83.   end; {if-else}
  84.  
  85.   __JustStr := InputStr;
  86.  
  87. End; {__JustStr}
  88.  
  89. { --------------------------------------------------------------------- }
  90.  
  91. Function __RemWhiteStr(InputStr:string;MyMode:byte):string;
  92.  
  93. { This function is useful for stripping spaces from a string.
  94.  
  95.   InputStr is the string that will be processed.
  96.  
  97.   MyMode is a constant, chosen from the list above, that chooses
  98.   whether the leading, trailing, or entire complement of spaces
  99.   is removed.
  100. }
  101.  
  102. Var
  103.  
  104.   Count : byte;                   {general purpose counter}
  105.   MyStr : string;                 {temporary string       }
  106.  
  107. begin
  108.  
  109.   if (MyMode=_Leading) then begin
  110.     {remove the leading spaces}
  111.     Count := 0;
  112.     while (InputStr[Count+1]=' ') AND (Count<Length(InputStr)) do begin
  113.       Count := Count + 1;
  114.     end; {while}
  115.     if Count > 0 then
  116.       Delete(InputStr,1,Count);
  117.   end; {if}
  118.  
  119.   if (MyMode=_Trailing) then begin
  120.     {remove the leading spaces}
  121.     Count := Length(InputStr);
  122.     while (InputStr[Count]=' ') AND (Count>0) do begin
  123.       Count := Count - 1;
  124.     end; {while}
  125.     if Count < Length(InputStr) then
  126.       Delete(InputStr,Count+1,Length(InputStr)-Count);
  127.   end; {if}
  128.  
  129.   if (MyMode=_All) then begin
  130.     MyStr := '';
  131.     for Count := 1 to Length(InputStr) do begin
  132.       if InputStr[Count] <> ' ' then
  133.         MyStr := MyStr + InputStr[Count];
  134.     end; {for}
  135.     InputStr := MyStr;
  136.   end; {if}
  137.  
  138.   __RemWhiteStr := InputStr;
  139.  
  140. end; {__RemWhiteStr}
  141.  
  142. { --------------------------------------------------------------------- }
  143.  
  144. end. {unit}
  145.